//
//  CoustomViewController.m
//  DongtanText
//
//  Created by 王恒 on 16/9/9.
//  Copyright © 2016年 wangheng. All rights reserved.
//

#import "CoustomViewController.h"
#import "CoustomTableViewCell.h"
#import "NSString+Height.h"

@interface CoustomViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    UITableView *_tableView;
}

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation CoustomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataArray = [[NSMutableArray alloc] init];
    [self addContentView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addContentView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height - 94) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    [footer setTitle:@"上拉可以加载更多数据" forState:MJRefreshStateIdle];
    [footer setTitle:@"松开即可加载更多数据" forState:MJRefreshStatePulling];
    [footer setTitle:@"加载数据中..." forState:MJRefreshStateRefreshing];
    _tableView.mj_footer = footer;
    MJRefreshNormalHeader *headr = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _dataArray = [[NSMutableArray alloc] init];
        [self getData];
    }];
    _tableView.mj_header = headr;
    [self.view addSubview:_tableView];
}

-(void)getData{
    NSLog(@"%@",_user);
    int page = (int)_dataArray.count/20;
    MyTool *tool = [[MyTool alloc] init];
    tool.completeRequest = ^(NSURLSessionDataTask *task, id responseObject){
        NSArray *dic = [responseObject objectForKey:@"tweetlist"];
        NSMutableArray *mutable = [tweetlistModel mj_objectArrayWithKeyValuesArray:dic];
        for (tweetlistModel *model in mutable) {
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    };
    [tool getTaskWithString:[NSString stringWithFormat:@"action/openapi/tweet_list?access_token=%@&pageSize=20&page=%d&dataType=json&user=%@",_token,page + 1,_user]];
}

#pragma tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    CoustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[CoustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //动态改变高度
    tweetlistModel *detail = _dataArray[indexPath.row];
    float h = [NSString getHeightOfString:detail.body withFont:[UIFont systemFontOfSize:14.0]];
    return 60 + h;
}

@end
